package com.bzkj.web.controller.warehouse;

import com.bzkj.common.annotation.LogC;
import com.bzkj.common.core.controller.BaseController;
import com.bzkj.basicinfo.domain.ncc.StoArea;
import com.bzkj.basicinfo.service.ncc.StoAreaService;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.common.core.page.TableDataInfo;  // 引入分页相关类
import com.bzkj.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * 库区信息 业务处理
 */
@Api(tags = "库区管理")
@RestController
@RequestMapping("/ncc/basic/stoArea")
public class StoAreaController extends BaseController
{

    @Autowired
    private StoAreaService stoAreaService;

    /**
     * @param stoArea 新库区信息
     * @return AjaxResult 返回添加结果信息
     * @method addStoArea
     * @description 添加新的库区
     */
    @PreAuthorize("@ss.hasPermi('index:region:add')")
    @ApiOperation("添加新的剂型")
    @PostMapping
    @LogC(title = "剂型管理", businessType = BusinessType.UPDATE, pkFieldNames = "id",
            tableNames = "basic_formulation_info"
    )
    public AjaxResult addStoArea(@RequestBody StoArea stoArea)
    {
        return stoAreaService.addStoArea(stoArea);
    }

    /**
     * @param stoArea 更新后的库区信息
     * @return AjaxResult 返回更新结果信息
     * @method updateStoArea
     * @description 更新库区信息
     */
    @PreAuthorize("@ss.hasPermi('index:region:edit')")
    @ApiOperation("更新库区信息")
    @PutMapping
//    @LogC(title = "货区管理", businessType = BusinessType.UPDATE, pkFieldNames = "sto_area_id",
//            tableNames = "basic_sto_area",
//            associatedIdField = "sto_area_id"
//    )
    public AjaxResult updateStoArea(@RequestBody StoArea stoArea)
    {
        return stoAreaService.updateStoArea(stoArea);

    }

    /**
     * 删除剂型
     */
    @ApiOperation("根据id删除剂型")
    @DeleteMapping("/{id}")
    public AjaxResult deleteStoArea(@PathVariable String id)
    {
        // 检查库区是否有关联的货位
        if(stoAreaService.hasUnits(id)) {
            return error("删除失败：库区有关联的货位，请先删除货位");
        }
        int result = stoAreaService.deleteFormulation(id);
        if(result > 0) {
            return success("删除库区成功");
        } else {
            return error("删除库区失败");
        }
    }

    /**
     * @param stoAreaId 库区ID
     * @return AjaxResult 返回库区信息
     * @method getStoAreaById
     * @description 根据ID获取库区信息
     */
    @ApiOperation("根据stoAreaId获取库区信息")
    @GetMapping("/{stoAreaId}")
    public AjaxResult getStoAreaById(@PathVariable String stoAreaId)
    {
        StoArea stoArea = stoAreaService.getStoAreaById(stoAreaId);
        if(stoArea != null) {
            return success(stoArea); // 返回成功，并返回库区数据
        } else {
            return error("库区不存在"); // 如果没有找到库区，返回错误信息
        }
    }

    /**
     * @param stoArea 查询条件（用于分页和筛选）
     * @return TableDataInfo 返回分页后的库区列表
     * @method getAllStoAreas
     * @description 获取所有库区信息，支持分页
     */
    @ApiOperation("获取所有库区信息，分页")
    @GetMapping
    public TableDataInfo getAllStoAreas(StoArea stoArea)
    {
        // 开始分页
        startPage(); // 获取分页信息

        // 查询库区列表
        List<StoArea> stoAreas = stoAreaService.getAllStoAreas(stoArea);

        // 返回分页后的数据
        return getDataTable(stoAreas);
    }


    /**
     * 在新增库区/货位的时候，前端显示自己仓库的name，和新增确定的时候给后端传递sto_id
     *
     * @return
     */
    @ApiOperation("在新增库区/货位的时候发送的请求，来展示仓库的name")
    @GetMapping("/basicStoStock")
    public AjaxResult getBasicStoStock()
    {
        return stoAreaService.getBasicStoStock();
    }

    /**
     * 在新增货位的时候，选择当前仓库下有哪些库区
     *
     * @param sto_id
     * @return
     */
    @ApiOperation("在新增货位的时候发送的请求，选择当前仓库下有哪些库区")
    @GetMapping("/addStoUnit/{sto_id}")
    public AjaxResult getAddStoUnitById(@PathVariable String sto_id)
    {
        return stoAreaService.getAddStoUnitById(sto_id);
    }


    @PreAuthorize("@ss.hasPermi('index:wareHouse:list')")
    @GetMapping("/all")
    public HashMap<String, Object> queryStoArea(String sto_code, @RequestParam(required = false, defaultValue = "") String sto_area_code)
    {
        HashMap<String, Object> response = new HashMap<>();
        //        调用函数直接返回
        List<HashMap<String, Object>> hashMaps = null;
        try {
            try {
                hashMaps = stoAreaService.queryStoAreaHouse(sto_code, sto_area_code);
                response.put("success", true);
                response.put("data", hashMaps);
                response.put("code", "200");
                response.put("message", "库区查询成功");
                response.put("errorStack", null);
                response.put("totalnum", hashMaps.size());

            } catch(Exception e) {
                response.put("success", false);
                response.put("data", null);
                response.put("code", "1000000001");
                response.put("message", "库区查询异常");
                response.put("errorStack", e);
                response.put("totalnum", 0);
            }
        } catch(Exception e) {
            e.printStackTrace();
        }

        return response;
    }
    @GetMapping("/getAllAreaRules")
    public AjaxResult getAllAreaRules(String stoAreaId)
    {
        return stoAreaService.getAllAreaRules(stoAreaId);
    }

    @ApiOperation("添加新的分类")
    @PostMapping("/addAreaRules")
    public AjaxResult addAreaRules(@RequestBody StoArea stoArea)
    {
        return stoAreaService.addAreaRules(stoArea);
    }

    @ApiOperation("删除分类")
    @DeleteMapping("/delAreaRules")
    public AjaxResult addAreaRules( String id)
    {
        return stoAreaService.delAreaRules(id);
    }

}
